package com.sofwin.controller;

import com.github.pagehelper.PageInfo;
import com.sofwin.Constants;
import com.sofwin.pojo.SysRole;
import com.sofwin.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("system/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 用于接收角色查询的请求
     * @param role
     * @param pageNumber
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("data")
    @RequiresRoles("1")
    public String data(SysRole role, @RequestParam(required = true,defaultValue = "1") Integer pageNumber, @RequestParam(required = true,defaultValue = Constants.DEFAULT_PAGE_SIZE) Integer pageSize, Model model)   {
        PageInfo<SysRole> page = roleService.selectByPage(role,pageNumber,pageSize);
        model.addAttribute("role",role);
        model.addAttribute("data",page.getList());
        model.addAttribute("page",page);
        return "role/data";
    }

    /**
     * 用于接收新增或编辑请求
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("toEdit")
    public String toEdit(Integer id,Model model){
        if(id!=null && id!=0){
            // 编辑
            SysRole role = roleService.selectById(id);
            model.addAttribute("role",role);
        }
        return "role/edit";
    }

    /**
     * 用于处理新增或编辑的请求
     * @param role
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public Map save(SysRole role){
        boolean status = roleService.saveRole(role);
        Map map = new HashMap();
        map.put("status",status);
        return map;
    }

    /**
     * 用于处理批量删除和单个删除
     * @param ids
     * @return
     */
    @RequestMapping("deletes")
    @ResponseBody
    public Map deletes(Integer[] ids){
        boolean status = roleService.deleteByIds(ids);
        Map map =new HashMap();
        map.put("status",status);
        return map;
    }

    @GetMapping("toPermission")
    public String toPermission(Integer roleId,Model model){
        model.addAttribute("roleId",roleId);
        return "role/permission";
    }
    @PostMapping("savePermission1")
    @ResponseBody
    public Map savePermission(Integer roleId,Integer resourceId,Integer flag){
        Map map = new HashMap();
        boolean status = roleService.savePermission(roleId,resourceId,flag);
        map.put("status",status);
        return map;
    }
    @RequestMapping("savePermission")
    @ResponseBody
    public Map saveRolePermission(Integer roleId,Integer[] resourceIds){
        // resourceIds数组接受的是所有当前选中的节点的id
        // 先将roleId对应的权限全部删除掉
        // 再将获取到的resourceIds和roleId循环插入到数据库中
        Map map = new HashMap();
        boolean status = roleService.savePermissions(roleId,resourceIds);
        map.put("status",status);
        return map;
    }
}
